package com.bot.rf.util;

import org.apache.log4j.Logger;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @program: report-form-demo
 * @description: 日志工具类
 * @author: zyy
 * @create: 2021-02-24 14:41
 **/
public class MyLogger {

    /**
     * 添加slf4j日志实例对象
     */
    public final static Logger LOGGER = Logger.getLogger(MyLogger.class);

    /**
     * 获得异常的堆栈信息
     *
     * @param t 异常
     * @return 异常信息
     */
    public static String getTrace(Throwable t){
        StringWriter stringWriter = new StringWriter();
        PrintWriter writer = new PrintWriter(stringWriter);
        t.printStackTrace(writer);
        StringBuffer buffer = stringWriter.getBuffer();
        String[] str = buffer.toString().split("\\r\\n\\t");
        buffer = new StringBuffer();

        buffer.append("Exception");
        for (String s : str) {
            if (buffer.length() > 200) {
                break;
            }
            buffer.append("\r\n\t").append(s);
        }
        writer.close();
        return buffer.toString();
    }
}
